//
//  BBNodeView.m
//  xNose
//
//  Created by ben smith on 9/7/08.
//  Copyright 2008 __MyCompanyName__. All rights reserved.
//

#import "BBNodeView.h"
#import "BBNodeController.h"
#import "BBNode.h"
#import "BBDefaultController.h"


@implementation BBNodeView

@synthesize backgroundGradient;
@synthesize node;
@synthesize backgroundRect;

- (id)initWithFrame:(NSRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code here.
			[self setAutoresizesSubviews:YES];
			[self setupBackgroundRect];
			[self setupBackgroundGradient];
			[self setupTextAttributes];
			[self setupView];
    }
    return self;
}

-(void)setupView
{
	
}

-(void)setNode:(BBNode*)aNode
{
	[node removeObserver:self forKeyPath:@"title"];

	[aNode retain];
	[node release];
	node = aNode;

	if (node == nil) return;
	aNode.view = self;
	[titleField setStringValue:node.title];
	[aNode addObserver:self forKeyPath:@"title" options:NSKeyValueObservingOptionNew context:nil];
}


- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
	if (node != nil) [titleField setStringValue:self.node.title];	
}

- (void)drawRect:(NSRect)rect 
{
	[self drawNode];
//	[[NSColor redColor] set];
//	[NSBezierPath strokeRect:self.bounds];
}

-(void)drawNode
{

}

-(NSFont*)font 
{
	return [BBDefaultController regularLargeFont];
}

-(NSColor*)fontColor 
{
	return [NSColor blackColor];
}

-(NSRect)textRect
{
	return self.bounds;
}

- (void)setupTextAttributes
{
	titleField = [[NSTextField alloc] initWithFrame:[self textRect]];
	[titleField setAlignment:NSCenterTextAlignment];
	[titleField setFont:[self font]];
	[titleField setTextColor:[self fontColor]];
	[titleField setDrawsBackground:NO];
	[titleField setBordered:NO];
	[titleField setSelectable:NO];
	[titleField setEditable:NO];
	[titleField setTarget:self];
	[titleField setAction:@selector(fieldDone:)];
	[titleField setDelegate:self];
	//[titleField setAutoresizingMask:NSViewMinYMargin];
	[self addSubview:titleField];
}	


-(void)setupBackgroundRect
{
	NSRect containRect = NSInsetRect(self.bounds, 1, 1);
	containRect.origin.x = floor(containRect.origin.x) + 0.5;
	containRect.origin.y = floor(containRect.origin.y) + 0.5;
	containRect.size.width = floor(containRect.size.width);
	containRect.size.height = floor(containRect.size.height);
	
	self.backgroundRect = [NSBezierPath bezierPathWithRoundedRect:containRect xRadius:10 yRadius:10];
}

- (void)fieldDone:(id)sender
{
	self.node.title = [titleField stringValue];
	[[NSNotificationCenter defaultCenter] postNotificationName:@"BBReleaseEditNotification" object:nil];	
}

- (void)controlTextDidEndEditing:(NSNotification *)aNotification
{
	self.node.title = [titleField stringValue];
}	

- (void)setupBackgroundGradient
{
	// create a basic gradient for the background of the view
	
	CGFloat red1   =   0.10;
	CGFloat green1 =   0.20;
	CGFloat blue1  =  0.250;
	
	CGFloat red2    =  0.55;
	CGFloat green2  =  0.55;
	CGFloat blue2   =  0.65;
	
	NSColor* gradientTop    = [NSColor colorWithCalibratedRed:red1 green:green1 blue:blue1 alpha:1.0];    
	NSColor* gradientBottom = [NSColor colorWithCalibratedRed:red2 green:green2 blue:blue2 alpha:1.0];
	
	NSGradient* gradient;
	gradient = [[NSGradient alloc] initWithStartingColor:gradientBottom endingColor:gradientTop];
	
	self.backgroundGradient = gradient;
	[gradient release];
}

- (void) dealloc
{
	[self setNode:nil];
	[[NSNotificationCenter defaultCenter] removeObserver:self];
	[textAttributes release];
	[super dealloc];
}



@end
